package leetcode;

import java.util.Arrays;

public class nineteen2 {
    public static int[] searchRange(int[] nums, int target) {
        if(nums==null||nums.length==0){
            return  new int[]{-1,-1};
        }
        int left=0;
        int right=nums.length-1;
        //寻找左端点
        while(left<right){
            //此时left是左端点位置，注意mid不是左端点位置
            int mid=left+(right-left)/2;
            if(nums[mid]<target){
                left=mid+1;
            }else{
                right=mid;
            }
        }
        //左端点位置
        int begin=(nums[left]==target?left:-1);
        left=0;
        right= nums.length-1;
        //寻找右端点
        while(left<right){
            int mid=left+(right-left+1)/2;
            if(nums[mid]>target){
                right=mid-1;
            }else{
                left=mid;
            }
        }
        //右端点位置
        int end=(nums[right]==target?right:-1);
        return new int[]{begin,end};

    }
    public static void main(String[] args) {
        int[] nums={5,7,7,8,8,10};
        int target=8;
        int[] ints = searchRange(nums, target);
        System.out.println(Arrays.toString(ints));
    }
}
